package org.example;


import lombok.extern.slf4j.Slf4j;
import org.example.annotation.RpcReference;
import org.example.annotation.RpcServerScan;
import org.example.registery.impl.LocalServerRegistryImpl;
import org.example.interfaces.HelloService;
import org.example.thread.TaskThreadPool;
import org.example.utils.NettyClientUtils;
import org.example.utils.NettyServiceUtils;

/**
 * @description:
 * @author: feixiang.li
 * @date: 2025-01-02 22:10
 */
@Slf4j
@RpcServerScan("org.example")
public class RpcClient {

    @RpcReference
    static HelloService helloService;

    public static void main(String[] args) throws Exception {
        // step 1: 因为使用的是本地的注册中心，所以先启动服务端
        TaskThreadPool.execute(RpcClient::initServer);
        // 等个1秒钟，等待服务端启动完成
        Thread.sleep(1000);

        // step 2: 启动客户端-向服务端注册自己的信息
        NettyClientUtils.start(new LocalServerRegistryImpl(), RpcClient.class);

        // step 3: 然后调用服务
        log.error("调用dubbo最后的结果:{}", helloService.sayHello("xxxx-测试"));
    }

    public static void initServer() {
        // 如何在静态类中获取本类的class
        // 启动服务端
        NettyServiceUtils nettyServiceUtils = new NettyServiceUtils("127.0.0.1", 9999, new LocalServerRegistryImpl(), RpcClient.class);
        nettyServiceUtils.start();
    }
}
